import "./patient_synthesis.less";
import { BasicForm, useForm, FormSchema } from "@/components/Form";
import {
	createPatients,
	addPatient_fams,
	addPatient_jobs,
	addPatient_insur,
	addPatient_cond,
	addEmgcontacts
} from "@/api/components/patientInformation";
import { message } from "antd";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { useState, useMemo } from "react";
import { useTranslation } from "react-i18next";
export default function patient_synthesis_form(props: any) {
	const { t } = useTranslation();
	let [tabKey, setTabKey] = useState("");
	let [patientId, setPatientId] = useState("");
	// let [patCardno, setPatCardno] = useState("");
	let utilA = "viewComponents.patientInformation";
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		console.log("data", data);
		setTabKey(data.tabKey);
		setPatientId(data.patientId);
		// setPatCardno(data.patCardno);
		resetFields();
	});
	const getTitle = useMemo(() => {
		switch (props.tabKey) {
			case "0":
				return t(`${utilA}.addPatientInfo`);
			case "1":
				return t(`${utilA}.addFamInfo`);
			case "2":
				return t(`${utilA}.addOccupationInfo`);
			case "3":
				return t(`${utilA}.addInsurInfo`);
			case "4":
				return t(`${utilA}.addHealthStatus`);
			case "5":
				return t(`${utilA}.addEmergencyPerson`);
			default:
				return t(`${utilA}.addFamInfo`);
		}
	}, [props.tabKey]);
	// 患者信息表单
	const thPatientInfoForm: FormSchema[] = [
		{
			label: t("felix_ui.pat_cardno"),
			field: "patCardno",
			component: "Input",
			// componentProps: { value: patCardno },
			rules: [
				{
					required: true,
					message: t("felix_ui.please_enter_cardnum")
				}
			]
		},
		{
			label: t("felix_ui.full_name"),
			field: "fullName",
			component: "Input"
		},
		{
			label: t("felix_ui.age"),
			field: "age",
			component: "Input"
		},
		{
			label: t("felix_ui.citizen_id"),
			field: "citizenId",
			component: "Input",
			rules: [
				{
					required: true,
					message: t("viewComponents.patientInformation.pleaseEnterID")
				},
				{
					pattern: /^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
					message: t("viewComponents.patientInformation.TheFormatOfIDIncorrect")
				}
			]
		},
		{
			label: t("felix_ui.gender"),
			field: "gender",
			component: "Select",
			componentProps: {
				options: [
					{
						label: t("felix_ui.woman"),
						value: "F"
					},
					{
						label: t("felix_ui.man"),
						value: "M"
					},
					{
						label: t("felix_ui.other"),
						value: "Other"
					}
				]
			}
		},
		{
			label: t("felix_ui.date_of_birth"),
			field: "birthday",
			component: "DatePicker",
			rules: [{ type: "object" as const, required: true, message: "Please select time!" }],
			componentProps: {
				format: "YYYY-MM-DD"
			}
		},
		{
			label: t("felix_ui.ethnicity"),
			field: "ethnicity",
			component: "Input"
		},
		{
			label: t("felix_ui.pat_address"),
			field: "addrCity",
			component: "Input"
		},
		{
			label: t("felix_ui.n_industry"),
			field: "occupation",
			component: "Input"
		},
		{
			label: t("felix_ui.phone_numb"),
			field: "phoneNumb",
			component: "Input"
		},
		{
			label: t("felix_ui.grp_email"),
			field: "emailAddr",
			component: "Input",
			rules: [{ required: true, message: t("felix_ui.please_enter_email"), type: "email" }]
		},
		{
			label: t("felix_ui.house_number"),
			field: "addrHouseno",
			component: "Input"
		},
		{
			label: t("felix_ui.province_city"),
			field: "addrProvince",
			component: "Input"
		}
	];
	// 亲属表单
	const thPatientFamsForm: FormSchema[] = [
		{
			label: t(`${utilA}.famCitizenId`),
			field: "famCitizenId",
			component: "Input",
			rules: [
				{
					required: true,
					message: t(`${utilA}.pleaseEnterID`)
				},
				{
					pattern: /^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
					message: t(`${utilA}.TheFormatOfIDIncorrect`)
				}
			]
		},
		{
			label: t(`${utilA}.famFullName`),
			field: "famFullName",
			component: "Input"
		},
		{
			label: t(`${utilA}.famPhoneNumb`),
			field: "famPhoneNumb",
			component: "Input"
		},
		{
			label: t(`${utilA}.famRelatives`),
			field: "famRelatives",
			component: "Select",
			componentProps: {
				options: [
					{ label: t(`${utilA}.fatherAndSon`), value: t(`${utilA}.fatherAndSon`) },
					{ label: t(`${utilA}.matherAndSon`), value: t(`${utilA}.matherAndSon`) },
					{ label: t(`${utilA}.brotherAndSister`), value: t(`${utilA}.brotherAndSister`) },
					{ label: t(`${utilA}.brother`), value: t(`${utilA}.brother`) },
					{ label: t(`${utilA}.friend`), value: t(`${utilA}.friend`) },
					{ label: t(`${utilA}.other`), value: t(`${utilA}.other`) }
				]
			}
		},
		{
			label: t(`${utilA}.famAddress`),
			field: "famAddress",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrProvince`),
			field: "addrProvince",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrCity`),
			field: "addrCity",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrDistrict`),
			field: "addrDistrict",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrCommunity`),
			field: "addrCommunity",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrWard`),
			field: "addrWard",
			component: "Input"
		},
		{
			label: t(`${utilA}.status`),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`${utilA}.open`), value: 0 },
					{ label: t(`${utilA}.close`), value: 1 }
				]
			}
		}
	];
	// 职业表单
	const thPatientJobsForm: FormSchema[] = [
		{
			label: t(`${utilA}.patLevel`),
			field: "patLevel",
			component: "Select",
			componentProps: {
				options: [
					{ label: t(`${utilA}.soldier`), value: "1" },
					{ label: t(`${utilA}.civilServant`), value: "2" },
					{ label: t(`${utilA}.teacher`), value: "3" }
				]
			}
		},
		{
			label: t(`${utilA}.occupation`),
			field: "occupation",
			component: "Input"
		},
		{
			label: t(`${utilA}.workplace`),
			field: "workplace",
			component: "Input"
		},
		{
			label: t(`${utilA}.addrWork`),
			field: "addrWork",
			component: "Input"
		},
		{
			label: t(`${utilA}.officePhone`),
			field: "officePhone",
			component: "Input"
		},
		{
			label: t(`${utilA}.salary`),
			field: "salary",
			component: "Input"
		},
		{
			label: t(`${utilA}.validFrom`),
			field: "validFrom",
			component: "DatePicker"
		},
		{
			label: t(`${utilA}.validTo`),
			field: "validTo",
			component: "DatePicker"
		},
		{
			label: t(`${utilA}.status`),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`${utilA}.open`), value: 0 },
					{ label: t(`${utilA}.close`), value: 1 }
				]
			}
		}
	];
	// 医保表单
	const thPatientInsurForm: FormSchema[] = [
		{
			label: t(`${utilA}.hiCode`),
			field: "hiCode",
			component: "Input",
			rules: [
				{
					required: true,
					message: t(`${utilA}.pleaseEnterInsurCode`)
				}
			]
		},
		{
			label: t(`${utilA}.validFrom`),
			field: "validFrom",
			component: "DatePicker"
		},
		{
			label: t(`${utilA}.validTo`),
			field: "validTo",
			component: "DatePicker"
		},
		{
			label: t(`${utilA}.insType`),
			field: "insType",
			component: "Select",
			componentProps: {
				options: [
					{ label: t(`${utilA}.commercialInsur`), value: "1" },
					{ label: t(`${utilA}.residentInsur`), value: "2" },
					{ label: t(`${utilA}.employeeInsur`), value: "3" }
				]
			}
		},
		{
			label: t(`${utilA}.duration5yr`),
			field: "duration5yr",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`${utilA}.yes`), value: "0" },
					{ label: t(`${utilA}.no`), value: "1" }
				]
			}
		},
		{
			label: t(`${utilA}.insCompany`),
			field: "insCompany",
			component: "Input"
		},
		{
			label: t(`${utilA}.waiverCostSharing`),
			field: "waiverCostSharing",
			component: "Input"
		},
		{
			label: t(`${utilA}.dateWaiver`),
			field: "dateWaiver",
			component: "DatePicker"
		},
		{
			label: t(`${utilA}.status`),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`${utilA}.open`), value: 0 },
					{ label: t(`${utilA}.close`), value: 1 }
				]
			}
		}
	];
	// 健康状态表单
	const thPatientCondsForm: FormSchema[] = [
		{
			label: t(`${utilA}.healthIndicators`),
			field: "healthIndicators",
			component: "Input"
		},
		{
			label: t(`${utilA}.temperature`),
			field: "temperature",
			component: "Input"
		},
		{
			label: t(`${utilA}.respiratory`),
			field: "respiratory",
			component: "Input"
		},
		{
			label: t(`${utilA}.bmiIdx`),
			field: "bmiIdx",
			component: "Input"
		},
		{
			label: t(`${utilA}.height_unit`),
			field: "height",
			component: "Input"
		},
		{
			label: t(`${utilA}.weight_unit`),
			field: "weight",
			component: "Input"
		},
		{
			label: t(`${utilA}.chestCfr`),
			field: "chestCfr",
			component: "Input"
		},
		{
			label: t(`${utilA}.waistCfr`),
			field: "waistCfr",
			component: "Input"
		},
		{
			label: t(`${utilA}.hipCfr`),
			field: "hipCfr",
			component: "Input"
		},
		{
			label: t(`${utilA}.chrAlcohol`),
			field: "chrAlcohol",
			component: "Input"
		},
		{
			label: t(`${utilA}.chrTobacco`),
			field: "chrTobacco",
			component: "Input"
		},
		{
			label: t(`${utilA}.medAllergy`),
			field: "medAllergy",
			component: "Input"
		}
	];
	// 紧急联系人表单
	const thPatEmgcontactsForm: FormSchema[] = [
		{
			label: t(`${utilA}.relationship`),
			field: "relationship",
			component: "Select",
			componentProps: {
				options: [
					{ label: t(`${utilA}.fatherAndSon`), value: t(`${utilA}.fatherAndSon`) },
					{ label: t(`${utilA}.matherAndSon`), value: t(`${utilA}.matherAndSon`) },
					{ label: t(`${utilA}.brotherAndSister`), value: t(`${utilA}.brotherAndSister`) },
					{ label: t(`${utilA}.brother`), value: t(`${utilA}.brother`) },
					{ label: t(`${utilA}.friend`), value: t(`${utilA}.friend`) },
					{ label: t(`${utilA}.other`), value: t(`${utilA}.other`) }
				]
			}
		},
		{
			label: t(`${utilA}.emgName`),
			field: "emgName",
			component: "Input"
		},
		{
			label: t(`${utilA}.emgContact`),
			field: "emgContact",
			component: "Input"
		},
		{
			label: t(`${utilA}.remark`),
			field: "remark",
			component: "InputTextArea"
		}
	];
	let utilSchemas = () => {
		switch (tabKey) {
			case "0":
				return thPatientInfoForm;
			case "1":
				return thPatientFamsForm;
			case "2":
				return thPatientJobsForm;
			case "3":
				return thPatientInsurForm;
			case "4":
				return thPatientCondsForm;
			case "5":
				return thPatEmgcontactsForm;
			default:
				return thPatientFamsForm;
		}
	};
	// 配置form
	const [formPatient_synthesis, { resetSchema, resetFields, validateFields }] = useForm({
		schemas: utilSchemas(),
		showActionButtonGroup: false,
		labelWidth: 150,
		baseColProps: { span: 24 }
	});

	const handleSubmit = async () => {
		let value = await validateFields();
		value.patientId = patientId;
		// value.patCardno = patCardno;
		let newValue;
		let res;
		switch (tabKey) {
			case "0":
				if (value.birthday) value.birthday = value.birthday.split(" ")[0];
				res = await createPatients(value);
				break;
			case "1":
				await addPatient_fams(value);
				break;
			case "2":
				if (value.validFrom > value.validTo) {
					message.error(t(`${utilA}.validFrom_validTo_work`));
					return;
				}
				newValue = { ...value, validTo: Date.parse(value.validTo), validFrom: Date.parse(value.validFrom) };
				await addPatient_jobs(newValue);
				break;
			case "3":
				if (Date.parse(value.validFrom) > Date.parse(value.validTo)) {
					message.error(t(`${utilA}.validFrom_validTo_insur`));
					return;
				}
				newValue = {
					...value,
					validTo: value.validTo.split(" ")[0],
					validFrom: value.validFrom.split(" ")[0],
					dateWaiver: value.dateWaiver.split(" ")[0]
					// validTo: Date.parse(value.validTo),
					// validFrom: Date.parse(value.validFrom),
					// dateWaiver: Date.parse(value.dateWaiver)
				};
				await addPatient_insur(newValue);
				break;
			case "4":
				await addPatient_cond(value);
				break;
			case "5":
				await addEmgcontacts(value);
				break;
			default:
				break;
		}
		message.success(t(`${utilA}.successfullyAdded`));
		closeDrawer();
		props.success && props.success(res, value.patCardno);
	};
	resetSchema(utilSchemas());
	return (
		<BasicDrawer title={getTitle} width="500" {...props} register={registerDrawer} showFooter={true} ok={handleSubmit}>
			<BasicForm register={formPatient_synthesis} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
}
